package com.chinda.modules.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.chinda.common.annotation.SysLog;
import com.chinda.common.enums.MessageEnum;
import com.chinda.common.exception.RRException;
import com.chinda.common.utils.R;
import com.chinda.modules.sys.dto.SysLogDTO;
import com.chinda.modules.sys.entity.SysLogEntity;
import com.chinda.modules.sys.qo.SysLogQO;
import com.chinda.modules.sys.service.SysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;


/**
 * 系统日志
 *
 * @author chinda
 * @date 2021-06-20 07:18:54
 */
@AllArgsConstructor
@RestController
@RequestMapping("/syslogs")
@Api(tags = "系统日志")
public class SysLogController {

    private final SysLogService sysLogService;

    /**
     * 分页
     *
     * @param qo
     * @return
     */
    @SysLog("查询系统日志分页")
    @PostMapping("/page")
    @ApiOperation(value = "查询分页信息")
    public R page(@ApiParam(value = "qo", name = "查询实例", required = true) @RequestBody SysLogQO qo) {
        return R.ok().set(sysLogService.page(qo));
    }

    /**
     * 获取详情
     *
     * @return
     */
    @SysLog("查询系统日志详情")
    @GetMapping("/{id}")
    @ApiOperation(value = "获取详情" )
    public R info(@PathVariable("id")  Long id) {
        SysLogEntity sysLog = sysLogService.getById(id);
        return R.ok().set(sysLog);
    }

    /**
     * 保存
     *
     * @return
     */
    @SysLog("保存系统日志")
    @PostMapping
    @ApiOperation(value = "保存信息")
    public R save(@ApiParam(value = "sysLogDTO", name = "系统日志DTO实例", required = true) @RequestBody @Valid SysLogDTO sysLogDTO) {
        SysLogEntity sysLog = new SysLogEntity();
        BeanUtil.copyProperties(sysLogDTO, sysLog, CopyOptions.create().ignoreNullValue());
        sysLogService.save(sysLog);
        return R.ok().set(sysLog);
    }

    /**
     * 修改全属性
     *
     * @param sysLogDTO
     * @return
     */
    @SysLog("修改系统日志全属性")
    @PutMapping("/{id}")
    @ApiOperation(value = "修改全属性")
    public R modify(@PathVariable("id") Long id,
                    @ApiParam(value = "sysLogDTO", name = "系统日志DTO实例", required = true) @RequestBody @Valid SysLogDTO sysLogDTO) {
        SysLogEntity sysLog = new SysLogEntity();
        BeanUtil.copyProperties(sysLogDTO, sysLog, CopyOptions.create().ignoreNullValue());
        sysLog.setId(id);
        if (!sysLogService.updateById(sysLog)) {
            throw new RRException(MessageEnum.SYS_CHECK_0012.getMsg());
        }
        return R.ok().set(sysLog);
    }

    /**
     * 修改局部属性
     *
     * @param id
     * @param sysLogDTO
     * @return
     */
    @SysLog("修改系统日志局部属性")
    @PatchMapping("/{id}")
    @ApiOperation(value = "修改局部属性")
    public R patch(@PathVariable("id") Long id,
                   @ApiParam(value = "sysLogDTO", name = "系统日志DTO实例", required = true) @RequestBody @Valid SysLogDTO sysLogDTO) {
        SysLogEntity dbSysLog = sysLogService.getById(id);
        if (dbSysLog == null) {
            throw new RRException(MessageEnum.SYS_CHECK_0022.getMsg());
        }
        BeanUtil.copyProperties(sysLogDTO, dbSysLog, CopyOptions.create().ignoreNullValue());
        if (!sysLogService.updateById(dbSysLog)) {
            throw new RRException(MessageEnum.SYS_CHECK_0012.getMsg());
        }
        return R.ok().set(dbSysLog);
    }

    /**
     * 删除数据
     *
     * @param id
     * @return
     */
    @SysLog("删除系统日志")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除数据")
    public R remove(@PathVariable("id") Long id) {
        sysLogService.removeById(id);
        return R.ok();
    }
    /**
     * 批量删除数据
     *
     * @param ids
     * @return
     */
    @SysLog("批量删除系统日志")
    @DeleteMapping("/batch")
    @ApiOperation(value = "批量删除数据")
    public R remove(@ApiParam(value = "ids", name = "id集合", required = true) @RequestBody Long [] ids) {
        sysLogService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
